/**
 * 
 */
package controller.xml;

import model.BasicResult;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;


/**
 * Use to parse a {@link BasicResult} in the exported session in XML.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class BasicResultXMLHandler extends XMLHandler {
	private BasicResult				basic	= new BasicResult();

	private BasicResultXMLStatus	status	= BasicResultXMLStatus.NONE;

	public BasicResultXMLHandler() {
	}


	@Override
	public boolean characters2(char[] ch, int start, int length) throws SAXException {

		switch (status) {
		case NONE:
			throw new SAXException("not supposed to receive character in this status");
		default:
			return super.characters2(ch, start, length);
		}
	}

	@Override
	public boolean endDocument2() throws SAXException {
		return false;
	}

	@Override
	public boolean endElement2(String uri, String localName, String qName) throws SAXException {
		switch (status) {
		case NONE:
			return false;

		case DESCRIPTION:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT_DESC))
			{// ferme description
				setDescription();
				status = BasicResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of DESCRIPTION was expected");

		case MEMORY:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT_MEMORY))
			{// ferme memory
				setMemory();
				status = BasicResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of MEMORY was expected");

		case STDERR:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT_STDERR))
			{// ferme stderr
				setStdErr();
				status = BasicResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of STDERR was expected");

		case STDOUT:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT_STDOUT))
			{// ferme stdout
				setStdOut();
				status = BasicResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of STDOUT was expected");

		case EXIT_VALUE:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT_EXITVALUE))
			{// ferme exitvalue
				setExitValue();
				status = BasicResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of EXITVALUE was expected");

		case BEGIN:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT_BEGIN))
			{// ferme begin
				setBegin();
				status = BasicResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of BEGIN was expected");

		case END:
			if (qName.equals(XMLBaliseFormat.BASIC_RESULT_END))
			{// ferme end
				setEnd();
				status = BasicResultXMLStatus.NONE;
				return true;
			}
			throw new SAXException("end of END was expected");
		}

		// attend une fin de balise qui existe pas...
		throw new SAXException("an other end of element was expected");
	}

	@Override
	public boolean startElement2(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException {
		if (status != BasicResultXMLStatus.NONE)
			throw new SAXException("already have a started element");

		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT_DESC))
		{// balise descritpion
			status = BasicResultXMLStatus.DESCRIPTION;
			return true;
		}
		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT_BEGIN))
		{// balise begin
			status = BasicResultXMLStatus.BEGIN;
			return true;
		}
		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT_END))
		{// balise end
			status = BasicResultXMLStatus.END;
			return true;
		}
		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT_EXITVALUE))
		{// balise exitvalue
			status = BasicResultXMLStatus.EXIT_VALUE;
			return true;
		}
		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT_STDOUT))
		{// balise stdout
			status = BasicResultXMLStatus.STDOUT;
			return true;
		}
		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT_STDERR))
		{// balise descritpion
			status = BasicResultXMLStatus.STDERR;
			return true;
		}
		if (arg2.equals(XMLBaliseFormat.BASIC_RESULT_MEMORY))
		{// balise descritpion
			status = BasicResultXMLStatus.MEMORY;
			return true;
		}
		// peut pas commencer balise qui existe pas
		throw new SAXException("unexpected balise: " + arg2);
	}

	/**
	 * Return the basic.
	 * 
	 * @return the basic
	 */
	public BasicResult getBasic() {
		return basic;
	}

	/**
	 * Return the status.
	 * 
	 * @return the status
	 */
	public BasicResultXMLStatus getStatus() {
		return status;
	}

	/**
	 * Set the description in the {@link BasicResult}
	 */
	private void setDescription() {
		basic.setDescription(buffer.toString());
	}

	/**
	 * Set the memeory used in the {@link BasicResult}
	 */
	private void setMemory() {
		basic.setMemoryUsed(buffer.toString());
	}

	/**
	 * Set the stdout in the {@link BasicResult}
	 */
	private void setStdOut() {
		basic.setStdout(buffer.toString());
	}

	/**
	 * Set the stderr in the {@link BasicResult}
	 */
	private void setStdErr() {
		basic.setStderr(buffer.toString());
	}

	/**
	 * Set the exit value in the {@link BasicResult}
	 * 
	 * @throws SAXException
	 */
	private void setExitValue() throws SAXException {
		int val = 0;
		try
		{
			val = Integer.valueOf(buffer.toString());
		} catch (Exception e)
		{
			throw new SAXException("bad integer value for EXIT_VALUE");
		}
		basic.setExitVal(val);
	}

	/**
	 * Set begin value in the {@link BasicResult}
	 * 
	 * @throws SAXException
	 */
	private void setBegin() throws SAXException {
		long begin = 0;
		try
		{
			begin = Long.valueOf(buffer.toString());
		} catch (Exception e)
		{
			throw new SAXException("bad long value for BEGIN");
		}
		basic.setBegin(begin);
	}

	/**
	 * Set end value in the {@link BasicResult}
	 * 
	 * @throws SAXException
	 */
	private void setEnd() throws SAXException {
		long end = 0;
		try
		{
			end = Long.valueOf(buffer.toString());
		} catch (Exception e)
		{
			throw new SAXException("bad long value for END");
		}
		basic.setEnd(end);
	}

}
